<template>
	<view style="padding: 10px;">
		<uni-forms :modelValue="formData">
			<uni-forms-item label="序号" name="serial_no" required label-align="right">
				<uni-easyinput type="text" v-model="formData.serial_no" placeholder="请输入序号" />
			</uni-forms-item>
			<uni-forms-item label="品名" name="name" required label-align="right">
				<uni-easyinput type="text" v-model="formData.name" placeholder="请输入品名" />
			</uni-forms-item>
			<uni-forms-item label="包码" name="code_bag" label-align="right">
				<view style="display: flex;flex-direction: row;">
					<uni-easyinput type="text" v-model="formData.code_bag" placeholder="请输入包码" />
					<button size="mini" @click="scanBagClick" type="primary" style="margin-left: 10rpx;  ">扫描</button>
				</view>
			</uni-forms-item>
			<uni-forms-item label="条码" name="code_carton" label-align="right">
				<view style="display: flex;flex-direction: row;">
					<uni-easyinput type="text" v-model="formData.code_carton" placeholder="请输入条码" />
					<button size="mini" @click="scanCartonClick" type="primary"
						style="margin-left: 10rpx;  ">扫描</button>
				</view>
			</uni-forms-item>
			<uni-forms-item label="零售价" name="price_sales" label-align="right">
				<uni-easyinput type="number" v-model="formData.price_sales" placeholder="请输入零售价" />
			</uni-forms-item>
			<uni-forms-item label="批发价" name="price_order" label-align="right">
				<uni-easyinput type="number" v-model="formData.price_order" placeholder="请输入批发价" />
			</uni-forms-item>

			<uni-forms-item>
				<view style="display: flex;flex-direction: row;justify-content: center;align-items: center;">
					<image style="border: 1px solid darkgray;" :src="formData.img" mode="aspectFit" class="item-img">
					</image>
				</view>
			</uni-forms-item>
			<uni-forms-item type="primary">
				<button type="primary" @click="handleImage">拍照 </button>
			</uni-forms-item>
		</uni-forms>
		<button type="primary" @click="submitForm">保存</button>
	</view>
</template>
<script>
	import dbOper from '@/module/accountSqlOper'
	import modal from "@/utils/modal.js"
	import {
		requestFileSystem,
		resolveLocalFileSystemURL,
		moveTo,
		getFile,
		createWriter,
		writerStrToFile
	} from "@/utils/plusIoPromise.js"
	export default {
		data() {
			return {
				formData: {
					id: null,
					serial_no: null,
					name: null,
					code_bag: null,
					code_carton: null,
					price_sales: null,
					price_order: null,
					img: null
				}
			}
		},
		onLoad(option) {
			if (option.id != -1) {
				this.formData.id = option.id
				this.getDate()
			}
		},
		methods: {
			scanBagClick() { //扫描包码
				uni.scanCode({
					scanType: ['barCode', 'qrCode', 'pdf417'],
				}).then(data => {
					var [err, res] = data;
					this.formData.code_bag = res.result
					this.searchClick()
				})
			},
			scanCartonClick() {
				uni.scanCode({
					scanType: ['barCode', 'qrCode', 'pdf417'],
				}).then(data => {
					var [err, res] = data;
					this.formData.code_carton = res.result
				})
			},
			getDate() {
				dbOper.selectById(this.formData.id)
					.then(res => {
						console.log("333", res);
						this.formData = res
					})
			},
			submitForm() {
				if (!this.formData.serial_no || !this.formData.name) {
					modal.msgError("请检查必填项")
					return
				}
				modal.confirm("是否要保存该数据")
					.then(res => {
						modal.loading()
						let fun = this.formData.id ? dbOper.updateRow(this.formData) : dbOper.insertRow(this.formData)
						fun.then(res => {
								modal.closeLoading()
								uni.navigateBack()
							})
							.catch(err => {
								modal.closeLoading()
							})
					})
			},
			handleImage() { //拍照
				uni.chooseImage({
					count: 1, // 选择一张图片
					sizeType: ['original', 'compressed'], // 可选择原图或压缩图
					sourceType: ['camera'], // 来源为相机
					success: (res) => {
						// 获取临时文件路径
						const tempFilePath = res.tempFilePaths[0];
						console.log('临时文件路径:', tempFilePath);
						// 将临时文件保存到指定目录
						this.saveImageToCustomDirectory(tempFilePath);
					},
					fail: (err) => {
						console.error('拍照失败:', err);
						uni.showToast({
							title: '拍照失败',
							icon: 'none'
						});
					}
				})
			},
			saveImageToCustomDirectory(tempFilePath) {
				const fileName = tempFilePath.substring(tempFilePath.lastIndexOf('/') + 1);
				let directory = ""
				requestFileSystem(plus.io.PUBLIC_DOCUMENTS)
					.then(fileSystem => {
						directory = fileSystem.root
						return resolveLocalFileSystemURL(tempFilePath)
					})
					.then(fileEntry => {
						return moveTo(fileEntry, directory, fileName)
					})
					.then(newEntry => {
						console.log(3, newEntry, newEntry.fullPath);
						this.formData.img = newEntry.fullPath
						console.log("保存位置", this.formData.img);
					})
					.catch(err => {
						console.log(4, err);
						uni.showToast({
							title: `图片保存失败: ${err}`,
							icon: 'none'
						})
					})
			},
		}
	}
</script>